// JavaScript Document

var jsonPriceData = null;

function imageThumbnailTable(jsonPriceData) {
  var html = "";
  html += '<table border=0 cellspacing=0 cellpadding=0 width=180 height=190>';
  html += '<tr><td align=center class=righttext>' + jsonPriceData.dimensions + '</td></tr>';	
  html += '<tr><td align=center>';
  if (jsonPriceData.useRCP) {
    html += '<img onContextMenu="return slideshowContextMenu()" style="border:1px solid black" src="/ftweb/bin/ft.dll/thumb?size=me&pictid=' + jsonPriceData.pictid + '">';
  } else {
    html += '<img style="border:1px solid black" src="/ftweb/bin/ft.dll/thumb?size=me&pictid=' + jsonPriceData.pictid + '">';  
  }
  html += '</td></tr>';
  html += '<tr><td align=center class=righttextdlg>' + jsonPriceData.filename + '</td></tr>';		
  html += '</table>';
  return(html);  
}

function printSizesAnchor(jsonPriceData, row) {
  var html = "";
  if (jsonPriceData.useNewPrintPreview) {
    html += '<input type=hidden value="" id="cCropRange' + jsonPriceData.pictid + '(' + row.itemid + ')"';
	html += ' name="crange' + jsonPriceData.pictid + '(' + row.itemid + ')"';
	html += '>';
    html += '<a href=\'javascript:newPrintPreview(';
	html += '{';
	html += ' pictureId: "' + jsonPriceData.pictid + '"';
	html += ', printWidth: ' + row.width;
	html += ', printHeight: ' + row.height;
  html += ', pageVer: 1';
	html += ', itemId: ' + row.itemid;
	html += ' , formName: "printsDlgFrm"';
	html += ', origWidth: ' + jsonPriceData.origWidth;
	html += ', origHeight: ' + jsonPriceData.origHeight;
	html += ', cropCbId: "iCrop' + jsonPriceData.pictid + '(' + row.itemid + ')"';	  
	html += ', quantityId: "qty' + jsonPriceData.pictid + '(' + row.itemid + ')"';
	html += ', rangeId: "cCropRange' + jsonPriceData.pictid + '(' + row.itemid + ')"';
  if (jsonPriceData.showPrintQuality) html += ', showPrintQuality: true'; else html += ', showPrintQuality: false';  
	html += '}';
	html += ')\''
	html += ' title="Click here to see a crop/fit preview of this print">';
  } else {
    html += '<a href="javascript:printPreview(\'' + jsonPriceData.pictid + '\',' + row.width + ',' + row.height + ',' + row.itemid + ',\'printsDlgFrm\',1)" title="Click here to see a crop/fit preview of this print">';
  }
  return(html);  
}

function printSizesHTML(jsonPriceData,sizesArray, tabName) {
  var html = "";
  var pricecnt = sizesArray.length;
  html += '<table><tr><td width=400>';
  html += '<input type=hidden name=pictid value="' + jsonPriceData.pictid + '">';
  html += '<input type=hidden name=ndx value="' + jsonPriceData.index + '">';	
  html += '<input type=hidden name=jsonMode value="1">';
  html += '<table border=0 width=100% cellspacing=0 cellpadding=2 class=dlgprintorder>';
  html += '<Th class=dlgprintorder><a name=prints>Size</a></th>';
  if (jsonPriceData.showPrintQuality) html += '<Th class=dlgprintorder>Quality</th>';
  html += '<Th class=dlgprintorder>Price</th>';
  html += '<Th class=dlgprintorder align=center>Crop</th>';
  html += '<Th class=dlgprintorder>Quantity</th>';
  html += '</TR>';
  if (pricecnt > 0)	{
	for (i = 0; i < pricecnt; i++ ) { 
	  var row = sizesArray[i]; 
      html += '<tr>';
      html += '<td class=dlgprintorder>' + row.name + '</td>';
      if (jsonPriceData.showPrintQuality) html += '<td class=dlgprintorder>' + row.quality + '</td>';
      html += '<td class=dlgprintorder>' + row.price + '</td>';
      html += '<td class=dlgprintorder align=center>';
      html += '<input class=dlgprintorder type=checkbox size=2 value=0 ';
      if (row.cropDflt) html += 'checked ';
      html += 'name="crop' + jsonPriceData.pictid + '(' + row.itemid + ')"';
      html += ' id="iCrop' + jsonPriceData.pictid + '(' + row.itemid + ')"';	  
	  html += '>';
      html += '<input type=hidden name="tab' + row.itemid + '" value="' + tabName + '">';    
      html += '&nbsp;&nbsp;&nbsp;&nbsp;';
	  html += printSizesAnchor(jsonPriceData, row);
      if (row.significantCrop) html += '<img border=0 src=/ftweb/images/ft/icons/cropred.gif>'; else html += '<img border=0 src=/ftweb/images/ft/icons/crop.gif>';
      html += '</a>';
      html += '</td>';
      html += '<td class=dlgprintorder><table border=0 cellspacing=0 cellpadding=0><tr><td><input class=dlgprintorder type=text size=2 value=0 name="' + jsonPriceData.pictid + '(' + row.itemid + ')"  id="qty' + jsonPriceData.pictid + '(' + row.itemid + ')" onfocus="SelectQty(this);"></td></tr></table></td>';
      html += '</tr>';
	}	  
  }

  html += "<tr><td colspan=5 class=dlgprintorder>";	
  html += '<table border=0 cellpadding=0 cellspacing=0 width=100%>';
  if (jsonPriceData.glossy) {
    html += '<tr><td class=dlgprintordertxt>I want&nbsp;&nbsp;';
    html += '<input class=rightcolortext type="radio" name="finish' + tabName + '" value="glossy" checked onclick="changeFinish(this)">glossy prints&nbsp;&nbsp;';
    html += '<input class=rightcolortext type="radio" name="finish' + tabName + '" value="matte" onclick="changeFinish(this)">matte prints</td></tr>';
  } else {
    html += '<tr><td class=dlgprintordertxt>I want&nbsp;&nbsp;'
    html += '<input class=rightcolortext type="radio" name="finish' + tabName + '" value="glossy" onclick="changeFinish(this)">glossy prints&nbsp;&nbsp;';
    html += '<input class=rightcolortext type="radio" name="finish' + tabName + '" value="matte" checked onclick="changeFinish(this)">matte prints</td></tr>';	
  }
  html += '</table>';
  html += '</td></tr>';
	
  html += '</table>';

  html += '</td>';
	
  html += '<td valign=middle width=190 style="padding-left:10px">';
  html += imageThumbnailTable(jsonPriceData);
  html += '</td>';
  html += '</tr></table>';

  return html;
}

function packagesHTML(jsonPriceData,sizesArray) {
  var html = "";
  var pricecnt = sizesArray.length;
  html += '<table border=0><tr><td width=400 valign=top>';
  html += '<input type=hidden name=pictid value="' + jsonPriceData.pictid + '">';
  html += '<input type=hidden name=ndx value="' + jsonPriceData.index + '">';	
  html += '<input type=hidden name=jsonMode value="1">';
  html += '<table border=0 width=100% cellspacing=0 cellpadding=2 class=dlgprintorder>';
  html += '<tr>';
  html += '<Th class=dlgprintorder><a name=prints>Packages</a></th>';
  html += '<Th class=dlgprintorder>Price</th>';
  html += '<Th class=dlgprintorder>Quantity</th>';
  html += '</TR>';
  if (pricecnt > 0)	{
	for (i = 0; i < pricecnt; i++ ) { 
	  var row = sizesArray[i]; 
      html += '<tr>';
      html += '<td class=dlgprintorder>' + row.name + '</td>';
      html += '<td class=dlgprintorder>' + row.price + '</td>';
      html += '<td class=dlgprintorder><table border=0 cellspacing=0 cellpadding=0><tr><td><input class=dlgprintorder type=text size=2 value=0 name="' + jsonPriceData.pictid + 'PKG(' + row.itemid + ')"  id="qty' + jsonPriceData.pictid + '(' + row.itemid + ')" onfocus="SelectQty(this);"></td></tr></table></td>';
      
      html += '<input type=hidden name="crop' + jsonPriceData.pictid + '(' + row.itemid + ')" value="';
      if (row.cropDflt)
        html += '1'
      else
        html += '0';
      html += '">';  
html += '</tr>';
	}	  
  }

  html += '</table>';
  html += '</td>';

  	
  html += '<td valign=middle width=190 style="padding-left:10px">';
  html += imageThumbnailTable(jsonPriceData);
  html += '</td>';
  html += '</tr></table>';

  return html;
}


function digitalDownloadsHTML(jsonPriceData,sizesArray) {
  var html = "";
  html += '<table>';  
  if (jsonPriceData.isGuest) {
    var pricecnt = sizesArray.length;
    html += '<tr><td width=400 valign=top>';
    html += '<input type=hidden name=pictid value="' + jsonPriceData.pictid + '">';
    html += '<input type=hidden name=ndx value="' + jsonPriceData.index + '">';	
    html += '<input type=hidden name=jsonMode value="1">';
    html += '<div class=righttextdlg style="color:#336699;font-weight:bold;padding-top:10px;padding-bottom:10px">Want to download the actual digital picture without the watermark? Pick your size here.</div>';
    html += '<table border=0 width=100% cellspacing=0 cellpadding=2 class=dlgprintorder>';
    html += '<Th class=dlgprintorder><a name=prints>Resolution</a></th>';
    html += '<Th class=dlgprintorder>Size</th>';
    html += '<Th class=dlgprintorder>Price</th>';
    html += '<Th class=dlgprintorder>Quantity</th>';
    html += '</TR>';
    if (pricecnt > 0)	{
	  for (i = 0; i < pricecnt; i++ ) { 
  	    var row = sizesArray[i]; 
        html += '<tr>';
        html += '<td class=dlgprintorder>' + row.name + '</td>';
        html += '<td class=dlgprintorder>' + row.size + '</td>';
        html += '<td class=dlgprintorder>' + row.price + '</td>';
        html += '<td class=dlgprintorder><table border=0 cellspacing=0 cellpadding=0><tr><td><input class=dlgprintorder type=text size=2 value=0 name="' + jsonPriceData.pictid + '(' + row.itemid + ')" onfocus="SelectQty(this);"></td></tr></table></td>';
        html += '</tr>';
	  }	  
    }
    html += '</table>';  	
	html += '<table border=0 cellspacing=0 cellpadding=0><tr><td class=righttextdlg style="padding-top:4px"><a class=bluelinkdlg href="javascript:viewLicense()">View License Agreement for digital downloads</a></td></tr></table>';
  } else {
    // not guest
    html += '<tr><td width=400 valign=top class=righttextbold style="padding-left:10px">';
	html += "<BR><BR><font color=#336699>Sorry, digital download purchases are only available to your guests, and cannot be purchased when logged in as an account administrator.</font>";	
  }
  html += '</td>';
	
  html += '<td valign=middle width=190 style="padding-left:10px">';
  html += imageThumbnailTable(jsonPriceData);  
  html += '</td>';
  html += '</tr></table>';

  return html;
}

function greetingCardHTML(jsonPriceData) {
  var html = "";
  html += '<table><tr><td width=400 class=righttextbold style="padding-left:10px">';
  html += '<a class=bluelinkdlg href="javascript:slideshow.makeCurrentCard()">';
  html += 'Click here to design your own custom<BR>greeting cards from this photo.';
  //html += '<img src="/ftweb/bin/ft.dll/greetingcardimage?pictid=' + jsonPriceData.pictid + '&clipart=3bb01h.png&center=1&crop=1&greetingimageborder=0&ext=.jpg&border=1&size=120">';
  html += '</a>';
  html += '</td>';
  html += '<td valign=middle width=190 style="padding-left:10px">';
  html += imageThumbnailTable(jsonPriceData);    
  html += '</td>';
  html += '</tr></table>';

  return html;
}

function giftHTML(jsonPriceData) {
  var html = "";
  html += '<table><tr><td width=400 class=righttextbold>';
  html += '<a class=bluelinkdlg href="javascript:slideshow.buyCurrentGift()">';  
  html += 'Click here to create your own custom photo<BR>gifts from this photo.  Choose from a variety<BR>of gifts like mugs, t-shirts, mousepads, and<BR>more.';
  html += '</a>';  
  html += '</td>';
  html += '<td valign=middle width=190 style="padding-left:10px">';
  html += imageThumbnailTable(jsonPriceData);    
  html += '</td>';
  html += '</tr></table>';
  return html;
}



var handleAddToCartFailure = function(o){
  alert("adding prints to cart failed");
}


var handleAddToCartSuccess = function(o){
  YAHOO.ft.buyPrintsDlg.hide();  
	try {slideshow.showMusicPlayer();	} catch(e) {};	
	if(o.responseText !== undefined){
	  if (!verifySessionOk(o.responseText,false)) return;
	  var tdiv =  document.getElementById("cartLogoutNav");
		tdiv.innerHTML = o.responseText;
	}
}

var callbackAddToCart =
{
  success:handleAddToCartSuccess,
  failure:handleAddToCartFailure
};

function addDlgToCart() {
	var sUrl = '/ftweb/bin/ft.dll/buyprintsjson';
	YAHOO.util.Connect.setForm("printsDlgFrm", false, false);
	//alert(YAHOO.util.Connect._sFormData);
	YAHOO.util.Connect.asyncRequest("POST", sUrl, callbackAddToCart); 
}

var preferredTab = "prints";

var handleBuyPrintsSuccess = function(o){
  //YAHOO.ft.buyPrintsDlg.show();  
  //alert(o.responseText);
  if(o.responseText !== undefined){
    if (!verifySessionOk(o.responseText,false)) return;
	if (jsonErrorCondition(o.responseText)) return;
    var tdiv =  document.getElementById("bpinsertspot");
    var thtml = "";
	var validJson = false;
    var firstchar = o.responseText.substring(0,1);
    if (firstchar == "{") {
	  var priceDataStr = o.responseText;
	  jsonPriceData = priceDataStr.parseJSON();
	  var frm = document.getElementById('prtdlgfrm');
	  frm.action = "/ftweb/bin/ft.dll/detailfs?slideshow=0&ndx=" + jsonPriceData.index;
	  var popSizesHtml = printSizesHTML(jsonPriceData, jsonPriceData.printSizes, 'popular'); 
	  var otherSizesHtml = printSizesHTML(jsonPriceData, jsonPriceData.otherPrintSizes, 'other');
	  var downloadSizesHtml = digitalDownloadsHTML(jsonPriceData, jsonPriceData.digitalDownloads);
	  var cardHtml = greetingCardHTML(jsonPriceData);
	  var giftHtml = giftHTML(jsonPriceData);
     var pkgHtml = packagesHTML(jsonPriceData, jsonPriceData.packages);
	  
	  var hasPrints = jsonPriceData.printSizes.length > 0;
	  var hasOtherPrintSizes = jsonPriceData.otherPrintSizes.length > 0;
	  var hasDigitalDownloads = jsonPriceData.digitalDownloads.length > 0; 	  
	  var hasGifts = jsonPriceData.hasGifts;
	  var hasCards = jsonPriceData.hasCards;
     var hasPackages = jsonPriceData.packages.length > 0;
	  
	  var tp = false, to = false, td = false, tg = false, tc = false; tpkg = false;
	  
     if ((hasPackages) && (preferredTab == "prints")) {
       tpkg = true;
     } else
	  if ((hasDigitalDownloads) && (preferredTab == "downloads")) {
	    td = true;
	  } else
	  if (hasPrints) {
	    tp = true; 
	  } else
	  if (hasOtherPrintSizes) {
	    to = true;
	  } else
	  if (hasDigitalDownloads) {
	    td = true;
	  } else
	  if (hasGifts) {
	    tg = true;
	  } else
	  if (hasCards) {
	    tc = true;
	  }

	  
	  validJson = true;
	} else {
      try { thtml = o.responseText;	} catch(e) { thtml = ""; }
    }
	
	if (validJson) {
      var tabView = new YAHOO.widget.TabView();
      if (hasPackages) tabView.addTab(new YAHOO.widget.Tab ( {label: 'Packages', content: pkgHtml, active:tpkg }));
      if (hasPrints) tabView.addTab(new YAHOO.widget.Tab ( {label: 'Popular Sizes', content: popSizesHtml, active:tp }));
      if (hasOtherPrintSizes) tabView.addTab(new YAHOO.widget.Tab ( {label: 'Other Sizes', content: otherSizesHtml, active:to }));
	  if (hasDigitalDownloads) tabView.addTab(new YAHOO.widget.Tab ( {label: 'Digital Downloads', content: downloadSizesHtml, active:td }));	  	  
	  if (hasGifts) {
	    var giftsTab = new YAHOO.widget.Tab ( {label: 'Merchandise/Gifts', content: giftHtml, active:tg });
		//giftsTab.addListener('click', function() { slideshow.buyCurrentGift() } );
		tabView.addTab(giftsTab);
	  }
	  if (hasCards) {
	    var cardsTab = new YAHOO.widget.Tab ( {label: 'Greeting Cards', content: cardHtml, active:tc });
		//cardsTab.addListener('click', function() { slideshow.makeCurrentCard() } );
		tabView.addTab(cardsTab);
	  }	  
	  tdiv.innerHTML = "";
	  tabView.appendTo("bpinsertspot");
	} else {
      tdiv.innerHTML = thtml;
      tdiv.scrollTop = 0;
	}
		
    var curl = document.getElementById("customurl");
    if (curl) {
	  url = curl.href;
	  var qp = url.indexOf('?');
	  if (qp >= 0) url = url.substring(0,qp);
	  url += "?name=" + jsonPriceData.filename + "&pictid=" + jsonPriceData.pictid;
      curl.href = url;
    }
    try {slideshow.hideMusicPlayer();	} catch(e) {};		
    YAHOO.ft.buyPrintsDlg.show();  
  }
}

var handleBuyPrintsFailure = function(o){
  alert("Unable to get price list from FotoTime for this picture");
}


var callbackBuyPrints =
{
  success:handleBuyPrintsSuccess,
  failure:handleBuyPrintsFailure
};


function closeBuyPrintsDlg() {
  YAHOO.ft.buyPrintsDlg.hide(); 
	try {slideshow.showMusicPlayer();	} catch(e) {};	
}

function BuyCurrentPrintDlg(pictid, ndx, albumid, groupid) {
    preferredTab = "prints";
    var sUrl = '/ftweb/bin/ft.dll/buyprintsjson?pictid=' + pictid + '&ndx=' + ndx + '&albumid=' + albumid + '&groupid=' + groupid;
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackBuyPrints); 
}

function BuyCurrentImageDlg(pictid, ndx, albumid, groupid) {
    preferredTab = "downloads";
    var sUrl = '/ftweb/bin/ft.dll/buyprintsjson?pictid=' + pictid + '&ndx=' + ndx + '&albumid=' + albumid + '&groupid=' + groupid;
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackBuyPrints); 
}


function viewLicense() {
  var sUrl = '/ftweb/bin/ft.dll/ddlicense?userid=' + slideshow.userid;
  var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackLicense); 
}

var callbackLicense =
{
  success:handleLicenseSuccess,
  failure:handleLicenseFailure
};

function handleLicenseFailure() {
  alert("Unable to retrieve license agreement from server.");
}

function handleLicenseSuccess(o) {
  var html = '<table border=0><tr><td><div class=ddLicense>' + o.responseText + '</div></td></tr></table>';
  showMessageDialog("License Agreement", html, "righttextdlg");  
}

function changeFinish(elem)
{
  return;
  var val = elem.value;
  var len = document.printsDlgFrm.length;
  if (val.indexOf('glossy') >= 0)
  {
   for(var i=0; i < len; i++) {
    var c = document.printsDlgFrm[i];
    if (c.type == 'radio')
    {
      if (c.value == 'glossy')
        c.checked = true;
    } 
   }
  }
  else
  {
    for(var i=0; i < len; i++) {
      var c = document.printsDlgFrm[i];
      if (c.type == 'radio')
      {
        if (c.value == 'matte')
          c.checked = true;
      } 
    }
  }
}